package com.example.demo.controller;

import com.example.demo.Student;
import com.example.demo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2022-12-01
 * Time: 15:32
 */

@Controller //我是一个控制器，我要在spring启动时加载并注册
@ResponseBody //当前类返回的是非静态页面，因为最早springmvc设计时是返回一个视图，所以默认是返回一个静态页面，要加注解才能返回数据；但是现在前后端分离了，要返回数据了
@RequestMapping("/web")
public class WebController {
    @RequestMapping("/hi")
    public Object sayHi() {
        return "Hi springMvc";
    }
    @RequestMapping(value = "/hello" ,method = RequestMethod.POST)
    public Object sayHello() {
        return "Hi SpringMVC";
    }
    @RequestMapping("/get1_0")
    public Object getParam1_0(HttpServletRequest request, HttpServletResponse response) {
        return "Hi" + request.getParameter("name");//通过servlet里的request获取参数，但是不推荐这样用
    }
    @RequestMapping("/get1")
    public Object getParam1(Integer age) {
       return "value"+age;
    }
    @RequestMapping("/get2")
    public Object getParam2(String name,String password) {
        return "name:: "+name + " | password:"+ password;
    }
    @RequestMapping("/get3")
    public String getParam3(Student student) {
        return student.toString();
    }
   @RequestMapping("/login")
    public String login(String name,String password) {
        return "<h1>用户名:" +name+" | 密码："+password+"</h1>";
    }
    @RequestMapping("/req")
    public String req(User user) {
        return user.toString();
    }
    @RequestMapping("/login3")
    public Map<String,Object> login3(@RequestBody User user) {
        Map<String,Object> result = new HashMap<>();
        result.put("name",user.getName());
        result.put("password",user.getPassword());
        return result;
    }
    @RequestMapping("/req2")
    public String req2(String name, @RequestPart("myfile")MultipartFile file) throws IOException {
        //保存文件
        file.transferTo(new File("D:/1/img.pong"));
        return "success";
    }
    @RequestMapping("/cookie")
    public String cookie(@CookieValue("username") String username) {
        return "cookie: "+username;

    }
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        // 获取 HttpSession 对象，参数设置为 true 表示如果没有 session 对象就创建session

        HttpSession session = request.getSession(true);
        if(session!=null){
            session.setAttribute("username","小卢");
        }
        return "session 存储成功";
    }
    @RequestMapping("/getSession")
    public String getSession(@SessionAttribute(value = "username",required = false) String name) {
        return "username: " + name;
    }
    @RequestMapping("/gettime")
    public String getTime(@RequestParam(value = "t",required = false)String time) {
        return "time: "+time;
    }
    @RequestMapping("/login4/{name}/{password}")
    public String login4(@PathVariable("name") String name,@PathVariable("password") String password) {
        return "name: "+name+" | password"+ password;
    }

}
